<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{


    function delete($user_id)
    {
        if (!$user_info = _model('user')->getInfoById($user_id)) {
            //@todo check msg
            $this->msg('user not exists');
        }
        _model('user')->delete(array('id'=>$user_id));
        $this->redirect();
    }

    function edit($user_id)
    {
        if (!$user_info = _model('user')->getInfoById($user_id)) {
            $this->msg('user not exists');
        }
        
        if (Request::isPost()) {
            $info = _POST('info', array());
            if (empty($info['user_name']) || empty($info['email'])) {
                // show msg
                exit(1);
            }
            if (empty($info['password'])) {
                unset($info['password']);
            } else {
                $info['password'] = md5($info['password']);
            }
            _model('user')->update_info($user_id, $info);
            $this->redirect('admin/registered_users.html');
        } else {
            $admin_group = $this->get_admin_group();
             if ($this->info['user_group']['id'] == 2) {
                 unset($admin_group[1], $admin_group[2]);
             }
             $this->view->assign('admin_group', $admin_group);
            $this->view->assign('info', $user_info);
            $this->view->display('admin/reg_user_edit.html');
        }
    }


    function index()
    {
        $f = _GET('f');
        $d = _GET('d');
        if (!$f) {
            $f = 'add_time';
        }
        
        if (!$d) {
            $d = 'desc';
        }
        
        $data = _model('user')->getList("WHERE user_group=0 ORDER BY $f $d", new Pager(20));

        $this->view->assign($data);
        $this->view->assign('d', $d);
        // $this->view->assign('admin_group', $this->get_admin_group());
        $this->view->display('admin/reg_user_manage.html');
    }
}

?>